package t20230601;

/**
 * @Author: DengJia
 * @Date: 2023/6/1
 * @Description: 不使用 LinkedList，来实现自己的内部链式存储机制
 */

public class LinkedStack<T> {
    private static class Node<U> {
        U element;
        Node<U> next;

        Node() {
            element = null;
            next = null;
        }

        public Node(U element, Node<U> next) {
            this.element = element;
            this.next = next;
        }

        boolean end() {
            return element == null && next == null;
        }
    }

    private Node<T> top = new Node<T>();

    public void push(T element) {
        top = new Node<T>(element, top);
    }

    public T pop() {
        T result = top.element;
        if (!top.end()) {
            top = top.next;
        }
        return result;
    }

    public static void main(String[] args) {
        LinkedStack<String> lss = new LinkedStack<>();
        String str = "hello thinking in java";
        for (String s : str.split(" ")) {
            lss.push(s);
        }
        String s;
        while ((s = lss.pop()) != null) {
            System.out.print(s + " ");
        }
    }
}
